package com.wty.swhn.wty.view;

import com.wty.swhn.wty.R;
import com.wty.swhn.wty.TApplication;
import com.wty.swhn.wty.biz.implAsmackImpl.LoginBiz;
import com.wty.swhn.wty.model.UserEntity;
import com.wty.swhn.wty.util.Const;
import com.wty.swhn.wty.util.PreferencesUtils;
import com.wty.swhn.wty.util.Tools;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.HandlerThread;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by swhn on 2016/8/4.
 */
public class LoginActivity extends BaseActivity {
	private Button but_register, but_login, but_hellp;
	private EditText edit_loginuser;
	private EditText edit_loginpassword;
	MyReceiver myReceiver;
	private String userName, passWord;
	private String userPassword;
	UserEntity userEntity;
	private final static String TAG = "LoginActivity";
	HandlerThread handlerThread = new HandlerThread("handler_thread");
	private long exitTime = 0;
	public LoginBiz loginBiz;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		setupView();

		myReceiver = new MyReceiver();
		IntentFilter intentFilter = new IntentFilter(Const.ACTION_lOGIN);
		this.registerReceiver(myReceiver, intentFilter);
	}

	private void setupView() {
		but_register = (Button) findViewById(R.id.but_regsiter);
		but_login = (Button) findViewById(R.id.but_login);
		but_hellp = (Button) findViewById(R.id.but_help);
		edit_loginpassword = (EditText) findViewById(R.id.edit_login_password);
		edit_loginuser = (EditText) findViewById(R.id.edit_login_user);
		// userName=edit_loginuser.getText().toString();
		// userPassword=edit_loginpassword.getText().toString();
		String username = PreferencesUtils.getSharePreStr(this, "username");// 用户名
		String pwd = PreferencesUtils.getSharePreStr(this, "pwd");// 密码
		if (!TextUtils.isEmpty(username)) {// 判断是否为空
			edit_loginuser.setText(username);
		}
		if (!TextUtils.isEmpty(pwd)) {
			edit_loginpassword.setText(pwd);
		}
		setOnClickListener listener = new setOnClickListener();

		but_register.setOnClickListener(listener);
		but_login.setOnClickListener(listener);
		but_hellp.setOnClickListener(listener);

	}

	private class setOnClickListener implements View.OnClickListener {
		int i = 0;

		@Override
		public void onClick(View view) {

			switch (view.getId()) {
			case R.id.but_regsiter:
				Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
				startActivity(intent);
				break;
			case R.id.but_login:
				String userName = edit_loginuser.getText().toString();
				String passWord = edit_loginpassword.getText().toString();
				StringBuffer buffer = new StringBuffer();
				PreferencesUtils.putSharePre(LoginActivity.this, "username", userName);
				PreferencesUtils.putSharePre(LoginActivity.this, "pwd", passWord);
				if (TextUtils.isEmpty(userName)) {
					buffer.append("用户名不能为空");
				}
				if (TextUtils.isEmpty(passWord)) {
					buffer.append("密码不能为空");
				}

				if (buffer.toString().length() > 0) {
					// 检查没能过
					Toast.makeText(LoginActivity.this, buffer, Toast.LENGTH_LONG).show();

					return;
				}

				UserEntity userEntity = new UserEntity(userName, passWord);
				// LoginBiz loginBiz = new LoginBiz(userEntity);
				// 如果不用LoginBiz,这里要改
				// new Thread(loginBiz).start();
				// 从工厂类拿对象
				// ILoginBiz iLoginBiz = Factory.newILoginBiz();
				// iLoginBiz.login(userEntity);
				// 单例模式
				// LoginBiz loginBiz = LoginBiz.getLoginBiz(userEntity);

				// loginBiz.login(userEntity);
				LoginBiz loginBiz = new LoginBiz(LoginActivity.this, userEntity);
				Tools.showProgressDialog(LoginActivity.this, "正努力登陆中。。。。");
				// but_login.setEnabled(true);
				// LoginActivity.this.loginBiz = new
				// LoginBiz(LoginActivity.this, userEntity, new Callback() {
				//
				// @Override
				// public void getBut(int b) {
				// // TODO Auto-generated method stub
				// if (b == 0) {
				//
				// but_login.setEnabled(false);
				// }
				//
				// }
				// });

				break;

			case R.id.but_help:
				break;

			}

		}

	}

	class MyReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				Tools.closeProgresDialog();
				int status = intent.getIntExtra(Const.KEY_DATA, -1);
				if (status == Const.STATUS_SUCCESS) {
					Tools.showMsg(LoginActivity.this, "成功====");
					Intent intent1 = new Intent(LoginActivity.this, TopickActivity.class);
					intent1.putExtra("USERID", userName);
					startActivity(intent1);
				}

				if (status == Const.CONNECT_FAILURE) {
					Toast.makeText(LoginActivity.this, "联网失败", Toast.LENGTH_LONG).show();
				}

				if (status == Const.LOGIN_FAILURE) {
					Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show();
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		String username = PreferencesUtils.getSharePreStr(this, "username");// 用户名
		String pwd = PreferencesUtils.getSharePreStr(this, "pwd");// 密码

		if (!TextUtils.isEmpty(username)) {// 判断是否为空
			edit_loginuser.setText(username);
		}
		if (!TextUtils.isEmpty(pwd)) {
			edit_loginpassword.setText(pwd);
		}
	}

	// 按返回按钮提示再按一次退出程序
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				TApplication.instance.exit();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
